/*
 * @lc app=leetcode.cn id=238 lang=java
 *
 * [238] 除自身以外数组的乘积
 */

// @lc code=start
class Solution {
    public int[] productExceptSelf(int[] nums) {
        // 前缀积和后缀积
        int n = nums.length;
        int left = 1, right = 1;
        int[] res = new int[n];
        for(int i=0; i<n; i++){
            res[i] = left;
            left *= nums[i];
        }
        for(int i=n-1; i>=0; i--){
            res[i] *= right;
            right *= nums[i];
        }
        return res;
    }
}
// @lc code=end

